<template>
  <div>
    <el-dialog height="500px" :title="title" :visible.sync="dialogFormVisible" width="500px" @close="close">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="姓名" prop="name">
          <el-input v-model.trim="form.name" autocomplete="off" :disabled="true" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model.number="form.phone" autocomplete="off" :disabled="true" placeholder="请输入手机号" type="number" />
        </el-form-item>
        <el-form-item label="新密码" prop="password">
          <el-input v-model.trim="form.password" autocomplete="off" placeholder="请输入新密码" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { API_userSetpassword } from '@/api/user'
export default {
  name: 'PasswordEdit',
  data() {
    return {
      form: {
        name: '',
        phone: '',
        password: '',
      },
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入姓名' }],
        phone: [{ required: true, trigger: 'blur', message: '请输入手机号' }],
        password: [{ required: true, trigger: 'blur', message: '请输入新密码' }],
      },
      title: '',
      dialogFormVisible: false,
    }
  },
  methods: {
    showEdit(row) {
      this.title = '设置密码'
      this.form = Object.assign({}, row)
      this.dialogFormVisible = true
    },
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
    },
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          const { message } = await API_userSetpassword({
            code: this.form.code,
            password: this.form.password
          })
          this.$baseMessage(message, 'success')
          this.dialogFormVisible = false
          this.close()
        } else {
          return false
        }
      })
    },
  },
}
</script>
